﻿// owner 蒋家伟

#include "SerialPortHardwareWorker.h"

// jsdk
#include "TaskId.h"
#include "Dbg_Warn_Define.h"
#include "IHardwareEventHandler.h"
#include "HardwareEventArgs.h"

using namespace jsdk;

OwnerPtr<SerialPortHardwareWorker> jsdk::SerialPortHardwareWorker::create(IHardwareEventHandler* pHardwareEventHandler)
{
    OwnerPtr<SerialPortHardwareWorker> opHardwareWorker = NEW_AS_OWNERPTR(SerialPortHardwareWorker);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opHardwareWorker, L"硬件设备工作对象", L"蒋家伟", L"2023-07-28");
    opHardwareWorker->m_opHardwareEvent.swap(IHardwareEvent::create());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opHardwareWorker->m_opHardwareEvent, L"硬件设备的事件初始化失败", L"蒋家伟", L"2023-07-28");
    opHardwareWorker->m_opHardwareEvent->registerHandler(pHardwareEventHandler);
    return std::move(opHardwareWorker);
}

void jsdk::SerialPortHardwareWorker::onAddTask(QVariant vTaskId)
{
    TaskId taskId = vTaskId.value<TaskId>();
    DBG_WARN_AND_RETURN_VOID_UNLESS(taskId.isValid(), L"传递一个无效的TaskId", L"蒋家伟", L"2023-07-31");
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_opHardwareEvent, L"硬件事件初始化失败", L"蒋家伟", L"2023-08-02");
    HardwareEventArgs tempArgs(HardwareEventType::NewTask, taskId);
    m_opHardwareEvent->sendEvent(&tempArgs);
}
